package com.randao.manage.firstPage.controller;

import com.randao.api.firstPage.HousesManageApi;
import com.randao.framework.common.exception.ExceptionCast;
import com.randao.framework.common.model.response.CommonCode;
import com.randao.framework.common.model.response.QueryResponseResult;
import com.randao.framework.common.model.response.QueryResult;
import com.randao.framework.model.domain.entity.JsonResult;
import com.randao.framework.model.domain.house.Houses;
import com.randao.framework.model.domain.productImg.ProductImg;
import com.randao.manage.firstPage.service.HousesManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-04月-16日  15:38
 * Look at the wine in the glass toast the rich first.
 */
@RestController
@RequestMapping("/houses")
public class HousesManageController implements HousesManageApi {

    @Autowired
    private HousesManageService housesManageService;



    /**
     * 查询全部楼盘
     *
     * @return
     */
    @Override
    @GetMapping("/findAll")
    public QueryResponseResult<Houses> findAll(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {

        return housesManageService.findAll(pageNum, pageSize);

    }


    /**
     * 查询某个楼盘
     *
     * @param hid   楼盘id（rd_houses）
     * @param fnum 房源楼层(rd_house)
     * @return
     */
    @Override
    @GetMapping("/findByHidAndFloor")
    public QueryResponseResult<Houses> findByHidAndFloor(@RequestParam("hid") String hid, @RequestParam("floor") String fnum) {

        if (fnum == null || "".equals(fnum) || StringUtils.isEmpty(fnum)) {
            fnum = "1";
        }

        List<Houses> housesList = housesManageService.findByHidAndFloor(hid, fnum);

        if (housesList.isEmpty()) {  //判断集合中有没有元素，有返回false，没有返回true
            ExceptionCast.cast(CommonCode.NO_QUERY_DATA);
        }
        //自己封装的类，里面有list属性，和total属性
        QueryResult<Houses> queryResult = new QueryResult<>();
        queryResult.setList(housesList);

        return new QueryResponseResult<>(CommonCode.SUCCESS, queryResult);

    }

    /**
     * 根据楼层筛选房源列表
     *
     * @param floor
     * @return
     */
    @Override
    @GetMapping("/findByFloor")
    public QueryResponseResult<Houses> findByFloor(String floor) {

        List<Houses> housesList = housesManageService.findByFloor(floor);
        if (housesList.isEmpty()) {
            ExceptionCast.cast(CommonCode.NO_QUERY_DATA);
        }
        QueryResult<Houses> queryResult = new QueryResult<>();
        queryResult.setList(housesList);

        return new QueryResponseResult<>(CommonCode.SUCCESS, queryResult);

    }

    /**
     * 根据房源id查询房源详情
     *
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<JsonResult> findById(String id) {
        JsonResult jr;
        try {
            Houses houses = housesManageService.findByHid(id);
            jr = new JsonResult(houses, "查询成功", true, 10000);
        } catch (Exception e) {
            e.printStackTrace();
            jr = new JsonResult(e.getClass().getName() + ":" + e.getMessage(), "查询失败", false, 11111);

        }
        return ResponseEntity.ok(jr);
    }

    /**
     * 展示图片
     *
     * @return
     */
    @Override
    @GetMapping("/findProductImg")
    public ResponseEntity<JsonResult> findProductImg(@RequestParam("id") String hid) {
        JsonResult jr;
        List<ProductImg> productImg = housesManageService.findProductImg(hid);
        jr = new JsonResult(productImg, "查询成功", true, 10000);
        return ResponseEntity.ok(jr);
    }
}
